#include "unordered_map.h"
#include "unordered_set.h"

// ...existing code...

// 覆盖：测试 unordered_map（包含 operator[] 的插入与更新）
void TestUnorderedMap()
{
    wk::unordered_map<std::string, int> m;

    // operator[] 会在不存在时插入默认值 0，并返回可写引用
    m["apple"]++;             // 0 -> 1
    m["banana"] += 2;         // 0 -> 2
    m["apple"] += 3;          // 1 -> 4
    int before = m["cherry"]; // 插入并得到默认 0
    std::cout << "[unordered_map] cherry before assign: " << before << "\n";
    m["cherry"] = 5;

    std::cout << "[unordered_map] contents: ";
    for (auto it = m.begin(); it != m.end(); ++it)
        std::cout << it->first << ":" << it->second << " ";
    std::cout << "\n";

    auto it = m.find("apple");
    std::cout << "[unordered_map] find(apple): " << (it != m.end());
    if (it != m.end())
        std::cout << ", value=" << it->second;
    std::cout << "\n";

    bool erased = m.erase("banana");
    std::cout << "[unordered_map] erase(banana): " << erased << "\n";
    std::cout << "[unordered_map] find(banana) after erase: " << (m.find("banana") != m.end()) << "\n";
}

// 覆盖：测试 unordered_set
void TestUnorderedSet()
{
    wk::unordered_set<int> s;
    int vals[] = {3, 5, 7, 3, 5, 9};
    for (int v : vals)
        s.insert(v);

    std::cout << "[unordered_set] contents: ";
    for (auto it = s.begin(); it != s.end(); ++it)
        std::cout << *it << " ";
    std::cout << "\n";

    std::cout << "[unordered_set] find(5): " << (s.find(5) != s.end()) << "\n";
    std::cout << "[unordered_set] erase(5): " << s.erase(5) << "\n";
    std::cout << "[unordered_set] find(5) after erase: " << (s.find(5) != s.end()) << "\n";
}

// ...existing code...

int main()
{
    // ...existing code...
    int a[] = {19, 30, 5, 36, 13, 20, 21, 12};
    wk::unordered_map<int, int> ht;
    const char *test[] = {"abcd", "derf"};
    // wk::unordered_map<std::string, std::string> ht1;

    // for (auto e : test)
    // {
    //     ht.insert({e, e});
    // }

    for (auto e : a)
    {
        ht.insert({e, e});
    }

    // ht.Insert({ 15, 15 });

    ht.erase(30);
    if (ht.find(20) != ht.end())
    {
        cout << "找到了" << endl;
    }

    if (ht.find(20) != ht.end())
    {
        cout << "找到了" << endl;
    }
    else
    {
        cout << "没有找到" << endl;
    }
    // int x = 1;
    // std::cout << (x << 1) << std::endl;
    // std::cout << (x) << std::endl;

    // 新增：调用测试函数
    TestUnorderedMap();
    TestUnorderedSet();

    return 0;
}
